# coding=utf-8
import datetime
import pymysql
import subprocess
import logging
import functools
import timeout_decorator
import time
import random
from pyppeteer_crawl_xmob import xmob
from crawl_toutiao import crawl_toutiao
from pyppeteer_crawl_baidu import zhiwei
from pyppeteer_crawl_baidu import zhiweiys
from pyppeteer_crawl_tuia import tuia
from cpa_select_insert import cpa
from aiohttp_post_baidu import xinpu,xinpuys
from concurrent.futures import ProcessPoolExecutor


logging.basicConfig(level=logging.INFO, format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',
                    datefmt='%a, %d %b %Y %H:%M:%S')


class check_etl_db:

    def __init__(self):

        self.host = "ip"
        self.port = 110
        self.user = "user"
        self.passwd = "passwd"
        self.db = "db"
        self.yesterday = (datetime.datetime.now() + datetime.timedelta(days=-1)).strftime('%Y%m%d')

    def select_mysql(self, big_channel, client_id):

        db = pymysql.connect(host=self.host, port=self.port, user=self.user, passwd=self.passwd, db=self.db)
        # 通过cursor创建游标
        cursor = db.cursor()
        try:
            sql = "select date,channel_id,client_id,cost from tb_channel_cost where channel_id='" + big_channel + "' and client_id = '" + client_id + "'  and date ='" + self.yesterday + "'"
            cursor.execute(sql)
            channel_id_tuple = cursor.fetchall()
            # 提交SQL
            db.commit()
        except Exception as e:
            print(datetime.datetime.now(), e)
            # 发生错误时回滚
            db.rollback()
            # 关闭数据库连接
        finally:
            db.close()
        return channel_id_tuple

    def check_and_alarm(self, big_channel, client_id):

        if not self.select_mysql(big_channel, client_id):
            print ("\t\tfirst")
            if client_id == 'cpa':
                cpa()
            if client_id == 'zhiweiys':
                zhiweiys()
            if client_id == 'zhiwei':
                zhiwei()
            if client_id == 'xinpu':
                xinpu()
            if client_id == 'xinpuys':
                xinpuys()
            if client_id == 'duocai':
                crawl_toutiao().exec_login_crawl()
            if client_id == 'jiuqi':
                xmob()
            if client_id == 'xm':
                tuia()
            time.sleep(round(random.uniform(4.71, 5.59), 1))

            if not self.select_mysql(big_channel, client_id):
                print ("\t\tsecond")
                if client_id == 'cpa':
                    cpa()
                if client_id == 'zhiweiys':
                    zhiweiys()
                if client_id == 'zhiwei':
                    zhiwei()
                if client_id == 'xinpu':
                    xinpu()
                if client_id == 'xinpuys':
                    xinpuys()
                if client_id == 'duocai':
                    crawl_toutiao().exec_login_crawl()
                if client_id == 'jiuqi':
                    xmob()
                if client_id == 'xm':
                    tuia()
                time.sleep(round(random.uniform(4.71, 5.59), 1))

                if not self.select_mysql(big_channel, client_id):
                    subprocess.call('curl -X POST "{https://api.im.baidu.com/api/msg/groupmsgsend?access_token'
                                    '=d926113cbf85c2df9ae5051fb495a9a83}" -H "Content-Type:application/json" -d \'{'
                                    '"message": '
                                    '{"body": [{"type": "TEXT", "content": "%s 没有%s数据! Please check !"}]}}\'' % (
                                        big_channel + "---" + client_id, self.yesterday), shell=True)
                    logging.info("{} 没有{}数据 ! Please check !".format(big_channel + "---" + client_id, self.yesterday))
                else:
                    logging.info("after second time retry " + self.yesterday+"---" + big_channel + "---" + client_id + " already exixts !")
            else:
                logging.info("after first time retry "+self.yesterday+"---"+big_channel+"---"+client_id+" already exixts !")
        else:
            logging.info(self.yesterday+"---"+big_channel+"---"+client_id+" already exixts !")

    def exec_main(self, big_channel, client_id):

        self.check_and_alarm(big_channel, client_id)


def timeout_exec(func):
    @functools.wraps(func)
    def wrpper(*args, **kw):
        try:
            func
            return func(*args, **kw)
        except:
            try:
                time.sleep(round(random.uniform(3.01, 4.89), 1))
                func
                return func(*args, **kw)
            except:
                try:
                    time.sleep(round(random.uniform(5.01, 6.89), 1))
                    func
                    return func(*args, **kw)
                except:
                    pass

    return wrpper


@timeout_exec
@timeout_decorator.timeout(60, use_signals=False)
def alarm_cpa():
    check_etl_db().exec_main(big_channel='toufang_cpa', client_id='cpa')


@timeout_exec
@timeout_decorator.timeout(120, use_signals=False)
def alarm_zhiweiys():
    check_etl_db().exec_main(big_channel='toufang_sembaidu', client_id='zhiweiys')


@timeout_exec
@timeout_decorator.timeout(120, use_signals=False)
def alarm_zhiwei():
    check_etl_db().exec_main(big_channel='toufang_sembaidu', client_id='zhiwei')


@timeout_exec
@timeout_decorator.timeout(120, use_signals=False)
def alarm_xinpuys():
    check_etl_db().exec_main(big_channel='toufang_sembaidu', client_id='xinpuys')


@timeout_exec
@timeout_decorator.timeout(120, use_signals=False)
def alarm_xinpu():
    check_etl_db().exec_main(big_channel='toufang_sembaidu', client_id='xinpu')


@timeout_exec
@timeout_decorator.timeout(120, use_signals=False)
def alarm_toutiao():
    check_etl_db().exec_main(big_channel='toufang_toutiao', client_id='duocai')


@timeout_exec
@timeout_decorator.timeout(120, use_signals=False)
def alarm_xmob():
    check_etl_db().exec_main(big_channel='toufang_xmob', client_id='jiuqi')


@timeout_exec
@timeout_decorator.timeout(120, use_signals=False)
def alarm_tuia():
    check_etl_db().exec_main(big_channel='toufang_tuia', client_id='xm')

# cpa
alarm_cpa()

# 百度
alarm_zhiweiys()
alarm_zhiwei()
alarm_xinpuys()
alarm_xinpu()

# 头条
alarm_toutiao()

# xmob
alarm_xmob()

# 推啊
alarm_tuia()
